home *** CD-ROM | disk | FTP | other *** search
-
-
- R A N D O M 2 - 2 5 4
-
- Program and Text by Bob Markland
-
-
- When we shuffle a deck of cards or watch a horse race we don't give
- much thought to the non-repeating random numbers reflected in the outcome.
- Even a tree shedding its leaves in the fall illustrates this principal.
- There is no certainty as to which leaf will fall first, in what order each
- will fall, and which will be last. But since no leaf can fall twice this
- is a non-repeating random number sequence.
-
- As programmers we must take non-repeating random numbers into account
- so that our programs accurately reflect the real world. It is incumbent
- upon us to realistically distribute numbers for a card shuffle or game
- simulation. Presentation of quiz questions or game levels in a non-
- repeating random order may sometimes be appropriate. Sooner or later
- you'll find need for RANDOM 2-254.
-
- By definition, non-repeating random numbers are a series of sequential
- numbers selected in random order. For example: the series 1-2-3 could be
- picked in the order 2-3-1 or 3-1-2 or 1-3-2, etc. To simulate a standard
- deck of cards you need 52 consecutive numbers, usually 1 through 52.
-
- There are several BASIC routines which use the Commodore RND function
- to accomplish this end. However, they tend to be painfully slow and I have
- found some which unexpectedly grind to a halt near the end while trying to
- find the last number or two which has not yet been used.
-
- RANDOM 2-254 is a small machine language program capable of simulating
- a card shuffle, for example, in less than a quarter of a second. It
- derives its random values from the CIA chip, which is said to provide a
- more statistically random number selection than the Commodore RND function.
- In addition to being quick it is also very versatile: You can specify a
- series of 2 to 254 numbers. The program can be loaded to many areas of
- free RAM in BANK 0 without modification. It's also compatible with other
- M/L routines in both 128 and 64 modes.
-
- The demo program which runs from LOADSTAR demonstrates three areas
- where RANDOM 2-254 is useful. It will be most beneficial to print a hard
- copy of this text and refer to it as you progress through the demo. You
- should also break out of the demo with RUN/STOP then LIST to learn how the
- routines utilize RANDOM 2-254 in different circumstances.
-
-
- GENERAL
- -------
-
- Lines 30 and 40 of the demo select the desired load address for RANDOM
- 2-254 and load it. The load address is contained in the variable ML and
- may be any address in BANK 0 free RAM as indicated below. Your particular
- program and its use of other M/L routines will dictate the best place to
- put RANDOM 2-254. This can include any unused portion of RAM from 2816
- ($0B00) to 7167 ($1BFF). Any area of RAM between the end of your BASIC
- program and 16383 ($3FFF) may be utilized. If you have used the GRAPHIC
- commands to move your BASIC program upward to $4000 and are not otherwise
- using the area for graphics, any part of the RAM from 7168 ($1C00) to 16383
- ($3FFF) is available for RANDOM 2-254.
-
- The memory required is 95 bytes for RANDOM 2-254 plus 1 byte for each
- number to be randomized. Thus, 254 numbers require 349 bytes of memory.
-
- Usually subroutines are loaded into memory at even multiples of 256.
- However, for the purpose of illustration, you will notice in line 30 that
- ML=2829. You can attach RANDOM 2-254 to the end of any other M/L routine
- if you are proficient with an M/L monitor. RANDOM 2-254 can also be called
- as a subroutine from within your own M/L program or routine
-
- FENDER'S NOTE: Usually you can assume that the end address of a machine
- language routine is the last byte it will be using, but not always. RANDOM
- 2-254 itself is a program that uses some memory directly after the end of
- the routine. If you place something directly after a machine language
- routine and the routine all of a sudden stops working correctly, suspect
- that the routine uses that area for something and move whatever you placed
- there.
-
-
- CARD GAMES
- ----------
-
- Lines 70-120 of the demo illustrate one method of defining a standard
- deck of 52 cards. In order to display 52 cards on the screen
- simultaneously, miniature cards are created with a custom font. Using this
- same font, and with a few simple changes in the PRINT statements
- demonstrated in lines 310-320 you can easily display the large cards common
- to LOADSTAR.
-
- In this demo the card back is not represented. It is customary to
- utilize the Zero (0) element of the CN( ) array to specify a card back.
-
- Although it is beyond the scope of this article, it is not too
- difficult to create non-standard card decks. By altering the arrays which
- represent the cards and their values you can create any deck -- 48 card
- Pinochle, 32 card Clobber, 104 card Canasta, 208 card Baccarat, multiple
- decks for casino Blackjack, an UNO(r) deck, or even Tarot cards.
-
- The first demo displays 52 cards as they would appear when you first
- open a factory fresh deck. Press [S] to shuffle and re-display the cards.
-
- Line 210 sets the variable N to 52, then passes the address to which
- you have loaded RANDOM 2-254 and tells it how many numbers to randomize.
-
- Line 220 randomizes the series of numbers. NOTE: SYSML is the ONLY
- command on the line necessary to RANDOM 2-254. The remaining variables in
- this line calculate the elapsed time required to complete the shuffle for
- the purpose of demonstration. In your own program, simply SYSML once each
- time you need to shuffle.
-
- RANDOM 2-254 generates a table of the randomized numbers. Line 230
- shows how to access the random sequence. The variable CN represents the
- definition of each card in its original sequence. The variable NU is the
- number of the card to be dealt. When NU=1 the top card on the deck is
- specified with CN=PEEK(ML+94+NU). When NU=2 the second card from the top
- of the shuffled deck is returned in CN, etc.
-
-
- QUIZZES or LEVELS
- -----------------
-
- To enhance learning it is desirable to present test questions in a
- different order each time the test is taken to prevent memorization of the
- sequence and rote answers. There are also applications in arcade type
- games where you may wish to randomly select different screens in a non-
- repeating order.
-
- Line 420 sets the parameters for 30 questions or levels. For 10, 50 or
- 200 questions/levels, adjust N accordingly.
-
- As above, SYSML is the only relevant statement in line 430. Use SYSML
- once at any time you wish to change the random order.
-
- In your own program you could use DATA statements to list 30 questions
- and 4 multiple-choice answers for each, then READ them into a 2-dimensional
- array. Use an incremented variable such as NU, as shown in line 440, to
- step through the random number table. In this case QN=PEEK(ML+94+NU) will
- return the number of the question/level to present next.
-
-
- KENO, BINGO, ETC.
- -----------------
-
- Several games of chance, including Keno, Bingo and Lotteries utilize
- non-repeating random numbers, as do several other applications.
-
- The third section of the demo illustrates how RANDOM 2-254 is used in
- Keno. The screen displays the 80 possible numbers in the game. After
- randomizing, the first 20 numbers are used to determine the outcome.
-
- A Keno layout consists of numbers from 1 to 80 as indicated by the
- variable N in line 610. For Bingo, which utilizes 99 possible numbers, set
- N=99. Once again, pass the load address for RANDOM 2-254 and tell the
- program how many numbers to randomize, using the indicated POKES.
-
- The object of Keno is for the player to pick from 1 to 10 numbers he
- believes will also be picked by the "ball machine" (computer). The "ball
- machine" then picks 20 random numbers and if the player makes the required
- number of matches he wins.
-
- For the Keno simulation set N=80 then SYSML. The first 20 numbers
- selected become relevant and the remainder are discarded. Thus, NU is
- incremented from 1 to 20 to return values in X, which would then be
- compared to the player's picks. For Bingo: set N=99, SYSML, then increment
- NU to return values in X until a Bingo occurs.
-
-
- OTHER USES
- ----------
-
- You will no doubt think of many other uses for non-repeating random
- numbers beyond those discussed here. Beyond simulations, there are
- numerous real-world applications.
-
- In elementary school one of the most humiliating experiences for
- children who lack coordination is the choosing of sides for team sports.
- The same people are always the last ones picked. Allowing a human to pick
- teams comes with built-in bias. Instead, for a class of 30, assign each
- person a number, then use RANDOM 2-254 in your program to randomize the 30
- numbers. The first 15 numbers picked are on one team and the second 15 are
- on the other.
-
- If you happen to be looking for a science project consider software
- which purports to pick winning Lottery numbers. Ask any statistician -- no
- matter which numbers were picked last week, last month, or last year
- (barring fraud or mechanical irregularity) every number has an equal chance
- of being picked the next time the game is played.
-
- To prove that Lottery picking software is little more than a scam, use
- it to pick "lucky" numbers, then write a program which sets up RANDOM 2-254
- to pick random numbers using the same criteria. With a statistically
- significant number of repetitions, you can easily prove that numbers picked
- at random are just as likely to win as those picked by Lottery software.
-
- Experiment with RANDOM 2-254 for your own applications. Or better yet,
- use it to develop a program that you can sell to LOADSTAR.
-
- \\\\\ R - Run RETURN - Menu \\\\\
-